﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;

namespace MVCnExt.Web.UI.Ext
{
    public class ExtRuntime : ExtControl, INamingContainer
    {
        private List<ExtComponent> _body = new List<ExtComponent>();
        private List<ScriptReference> _references = new List<ScriptReference>();

        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.util.Observable", this.ClientID, this.ID));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            //add items
            if (body.Count != 0)
            {
                // build items list
                StringBuilder itemList = new StringBuilder();

                for (int i = 0; i < body.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        itemList.Append(",");
#if DEBUG
                        itemList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = body[i].GetScriptDescriptors();

                    // get the items's list of script dependencies
                    List<ScriptReference> references = body[i].GetScriptReferences();

                    //make sure the script dependencies are registered
                    _references.AddRange(references);

                    // write the item's config to the list
                    itemList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.FunctionNew));
                }

                desc.AddPropertyBase("body", itemList);
            }

            return list;
        }


        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public virtual List<ExtComponent> body
        {
            get
            {
                return _body;
            }
        }


    }
}
